﻿Imports DotNetNuke
Imports DotNetNuke.Framework
Imports DotNetNuke.Entities.Modules.Actions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports NEvoWeb.Modules.NB_Store.SharedFunctions
Imports NEvoWeb.Modules.NB_Store


Public Class View
    Inherits DnnC_Accordion_CategoryMenuModuleBase

    Public _TabId As Integer = -1
    Public _SubMenuOnly As Boolean = False

#Region "Event methods"

    Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        If Not _SubMenuOnly Then
            _TabId = TabId
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            If Not Page.IsPostBack Then
                ShowAccordion()
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

#End Region

#Region "private methods"

    Private Sub ShowAccordion()
        Dim objCtrl As New CategoryController
        Dim aryList As ArrayList
        Dim strHtml As String = ""

        aryList = objCtrl.GetCategories(PortalId, GetCurrentCulture)
        If CInt(Settings("dnnC_defaultMenu")) = 0 Then
            strHtml = "<div class=""" & CStr(Settings("dnnC_AccordionStyle")) & " DnnC_CatContainer"">" & BuildAccordionMenu(aryList, "", 0, -1) & "</div>"
        Else
            strHtml = "<div class=""" & CStr(Settings("dnnC_AccordionStyle")) & " DnnC_CatContainer"">" & BuildAccordionMenu(aryList, "", CInt(Settings("dnnC_defaultMenu")), -1) & "</div>"
        End If

        phAccordionMenu.Controls.Add(New LiteralControl(strHtml))
        SetUpSettings()
    End Sub

    Private Function BuildAccordionMenu(ByVal aryList As ArrayList, ByVal htmlText As String, ByVal ParentID As Integer, ByVal AccordionLevel As Integer) As String
        Dim objCInfo As NB_Store_CategoriesInfo
        AccordionLevel += 1
        Dim strHeader As String = ""
        Dim strFooter As String = "</ul>"

        'If ParentID = 0 Then
        If AccordionLevel = 0 Then
            strHeader &= "<ul id=""accordion"" class=""accordion"">"
        Else
            strHeader &= "<ul>"
        End If

        For Each objCInfo In aryList
            If objCInfo.ParentCategoryID = ParentID And objCInfo.Archived = False And objCInfo.Hide = False Then
                htmlText &= "<li>" & BuildHtmlCatLink(objCInfo)
                If AccordionLevel < 50 Then ' stop infinate loop
                    htmlText &= BuildAccordionMenu(aryList, "", objCInfo.CategoryID, AccordionLevel + 1)
                End If

                htmlText &= "</li>"
            End If        Next

        If htmlText <> "" Then htmlText = strHeader & htmlText & strFooter

        Return htmlText
    End Function

    Private Function BuildHtmlCatLink(ByVal itemInfo As NB_Store_CategoriesInfo) As String
        Dim strHtmlOut As String = ""
        Dim strHtmlLink As String = ""
        Dim strHtmlText As String = ""
        Dim NonEmptyCat As Integer

        NonEmptyCat = itemInfo.CategoryID

        'If itemInfo.SEOName <> "" Then
        'strHtmlLink = GetProductListUrlByCatID(PortalId, _TabId, NonEmptyCat, itemInfo.SEOName, GetCurrentCulture)
        'Else
        'strHtmlLink = GetProductListUrlByCatID(PortalId, _TabId, NonEmptyCat, itemInfo.CategoryName, GetCurrentCulture)
        'End If

        If itemInfo.SEOName <> "" Then
            strHtmlLink = GetProductListUrlByCatID(PortalId, CType(Settings("lstProductTabs"), Integer), NonEmptyCat, itemInfo.SEOName, GetCurrentCulture)
        Else
            strHtmlLink = GetProductListUrlByCatID(PortalId, CType(Settings("lstProductTabs"), Integer), NonEmptyCat, itemInfo.CategoryName, GetCurrentCulture)
        End If










        strHtmlOut = "<a href=""" & strHtmlLink & """ "

        If CBool(Settings("dnnC_ShowProductCount")) And Not Settings("dnnC_ShowProductCount") = Nothing Then
            strHtmlOut &= ">" & itemInfo.CategoryName & " (" & itemInfo.ProductCount & ")</a>"
        Else
            strHtmlOut &= ">" & itemInfo.CategoryName & "</a>"
        End If

        Return strHtmlOut
    End Function

    Private Sub SetUpSettings()
        Dim strSettings As New StringBuilder
        strSettings.AppendLine("<script type=""text/javascript"">")
        strSettings.AppendLine("    jQuery(document).ready(function ($) {")
        strSettings.AppendLine("        $('#accordion').dcAccordion({")

        If Not CStr(Settings("dnnC_AccordionClassParent")) = "" Or Not CStr(Settings("dnnC_AccordionClassParent")) = Nothing Then
            strSettings.AppendLine("        classParent:  '" & CStr(Settings("dnnC_AccordionClassParent")) & "',")
        End If
        If Not CStr(Settings("dnnC_AccordionClassActive")) = "" Or Not CStr(Settings("dnnC_AccordionClassActive")) = Nothing Then
            strSettings.AppendLine("        classActive:  '" & CStr(Settings("dnnC_AccordionClassActive")) & "',")
        End If
        If Not CStr(Settings("dnnC_AccordionClassArrow")) = "" Or Not CStr(Settings("dnnC_AccordionClassArrow")) = Nothing Then
            strSettings.AppendLine("        classArrow:  '" & CStr(Settings("dnnC_AccordionClassArrow")) & "',")
        End If
        If Not CStr(Settings("dnnC_AccordionClassExpand")) = "" Or Not CStr(Settings("dnnC_AccordionClassExpand")) = Nothing Then
            strSettings.AppendLine("        classExpand:  '" & CStr(Settings("dnnC_AccordionClassExpand")) & "',")
        End If

        If Not CStr(Settings("dnnC_AccordionEventType")) = "" Or Not CStr(Settings("dnnC_AccordionEventType")) = Nothing Then
            strSettings.AppendLine("        eventType:  '" & CStr(Settings("dnnC_AccordionEventType")) & "',")
        Else
            strSettings.AppendLine("        eventType:  'click',")
        End If

        If Not CStr(Settings("dnnC_AccordionHoverDelay")) = "" Or Not CStr(Settings("dnnC_AccordionHoverDelay")) = Nothing Then
            strSettings.AppendLine("        hoverDelay: " & CStr(Settings("dnnC_AccordionHoverDelay")) & ",")
        Else
            strSettings.AppendLine("        hoverDelay: 100,")
        End If

        If Not CStr(Settings("dnnC_AccordionMenuClose")) = "" Or Not CStr(Settings("dnnC_AccordionMenuClose")) = Nothing Then
            strSettings.AppendLine("        menuClose: " & LCase(CBool(Settings("dnnC_AccordionMenuClose"))) & ",")
        Else
            strSettings.AppendLine("        menuClose: true,")
        End If

        If Not CStr(Settings("dnnC_AccordionAutoClose")) = "" Or Not CStr(Settings("dnnC_AccordionAutoClose")) = Nothing Then
            strSettings.AppendLine("        autoClose: " & LCase(CBool(Settings("dnnC_AccordionAutoClose"))) & ",")
        Else
            strSettings.AppendLine("        autoClose: true,")
        End If

        If Not CStr(Settings("dnnC_AccordionAutoExpand")) = "" Or Not CStr(Settings("dnnC_AccordionAutoExpand")) = Nothing Then
            strSettings.AppendLine("        autoExpand: " & LCase(CBool(Settings("dnnC_AccordionAutoExpand"))) & ",")
        Else
            strSettings.AppendLine("        autoExpand: false,")
        End If

        If Not CStr(Settings("dnnC_AccordionSpeed")) = "" Or Not CStr(Settings("dnnC_AccordionSpeed")) = Nothing Then
            strSettings.AppendLine("        speed: '" & CStr(Settings("dnnC_AccordionSpeed")) & "',")
        Else
            strSettings.AppendLine("        speed: 'slow',")
        End If

        If Not CStr(Settings("dnnC_AccordionSaveState")) = "" Or Not CStr(Settings("dnnC_AccordionSaveState")) = Nothing Then
            strSettings.AppendLine("        saveState: " & LCase(CBool(Settings("dnnC_AccordionSaveState"))) & ",")
        Else
            strSettings.AppendLine("        saveState: true,")
        End If

        If Not CStr(Settings("dnnC_AccordionDisableLinks")) = "" Or Not CStr(Settings("dnnC_AccordionDisableLinks")) = Nothing Then
            strSettings.AppendLine("        disableLink: " & LCase(CBool(Settings("dnnC_AccordionDisableLinks"))) & ",")
        Else
            strSettings.AppendLine("        disableLink: false,")
        End If


        strSettings.AppendLine("        showCount:  false,")

        If Not CStr(Settings("dnnC_AccordionCookie")) = "" Or Not CStr(Settings("dnnC_AccordionCookie")) = Nothing Then
            strSettings.AppendLine("        cookie:  '" & CStr(Settings("dnnC_AccordionCookie")) & "'")
        Else
            strSettings.AppendLine("        cookie:  'NBS_CateMenu_" & ModuleId & "'")
        End If

        strSettings.AppendLine("        });")
        strSettings.AppendLine("    });")
        strSettings.AppendLine("</script><!-- test 4 -->")

        ltlScriptInit.Text = strSettings.ToString



    End Sub

#End Region

End Class